package com.community.process._1010.common.utils;


import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitUtil {

    private static RabbitTemplate rabbitTemplate;

    @Autowired
    public void setUserMessageService(RabbitTemplate rabbitTemplate){
        RabbitUtil.rabbitTemplate = rabbitTemplate;
    }

    public static void sendDelayedMessage(String exchangeName,String routingKey,String message,Integer delayTime) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> {
            msg.getMessageProperties().setContentType("application/json");
            msg.getMessageProperties().setContentEncoding("UTF-8");
            msg.getMessageProperties().setDelay(delayTime*1000);
            return msg;
        });
    }

    public static void sendDelayedMessage(String exchangeName,String routingKey,Object message,Integer delayTime) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> {
            msg.getMessageProperties().setDelay(delayTime*1000);
            return msg;
        });
    }

    public static void sendDelayedMessage(String exchangeName, String routingKey, String message, Integer delayTime, CorrelationData correlationData) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> {
            msg.getMessageProperties().setContentType("application/json");
            msg.getMessageProperties().setContentEncoding("UTF-8");
            msg.getMessageProperties().setDelay(delayTime*1000);
            return msg;
        },correlationData);
    }

    public static void sendDelayedMessage(String exchangeName, String routingKey, Object message, Integer delayTime, CorrelationData correlationData) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> {
            msg.getMessageProperties().setDelay(delayTime*1000);
            return msg;
        },correlationData);
    }

    public static void sendMessage(String exchangeName,String routingKey,String message) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> {
            msg.getMessageProperties().setContentType("application/json");
            msg.getMessageProperties().setContentEncoding("UTF-8");
            return msg;
        });
    }

    public static void sendMessage(String exchangeName,String routingKey,Object message) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> msg);
    }

    public static void sendMessage(String exchangeName,String routingKey,String message, CorrelationData correlationData) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> {
            msg.getMessageProperties().setContentType("application/json");
            msg.getMessageProperties().setContentEncoding("UTF-8");
            return msg;
        },correlationData);
    }

    public static void sendMessage(String exchangeName,String routingKey,Object message, CorrelationData correlationData) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message,msg -> msg,correlationData);
    }
}
